<?php

namespace Admin\Controller;

use Component\AdminController;

class AuthController extends AdminController
{
    //管理权限页
    function showlist()
    {
        $auth = D('Auth');
        $total = $auth->count();
        $per = 10;
        $page = new \Component\Page($total, $per);
        $sql = "select * from sw_auth ";
        $sql = $sql . $page->limit;
        $info = $auth->query($sql);
        $pagelist = $page->fpage();
        $this->assign('pagelist', $pagelist);
        $this->assign('info', $info);
        $this->display();
    }

    //添加权限
    function add()
    {
        if (!empty($_POST)) {
            //在AuthModel里边通过一个指定方法实现权限添加
            if (trim($_POST['auth_name']) != "" && trim($_POST['auth_c']) != "" && trim($_POST['auth_a']) != "" && trim($_POST['auth_level']) != "") {
                $auth = new \Model\AuthModel();
                $auth->create();
                $z = $auth->add();
                if ($z) {
                    $this->redirect('showlist');
                } else {
                    $this->assign("error", "添加权限失败，此权限是否已存在？");
                }
            } else {
                $this->assign("error", "存在输入为空！");
            }
        }
        $this->display();
    }

    //删除权限
    function del($auth_id)
    {
        $auth = D('Auth');
        $rst = $auth->delete($auth_id);
        if ($rst > 0) {
            $this->redirect('showlist');
        } else {
            $this->redirect('showlist', '权限删除失败！');
        }
    }

    //修改权限
    function upd($auth_id)
    {
        $auth = D('Auth');
        $ainfo = $auth->find($auth_id);
        $this->assign("ainfo", $ainfo);

        if (!empty($_POST)) {
            if (trim($_POST['auth_name']) != "" && trim($_POST['auth_c']) != "" && trim($_POST['auth_a']) != "" && trim($_POST['auth_level']) != "") {
                $auth->create();
                $rst = $auth->save();
                if ($rst > 0) {
                    $this->redirect('showlist');
                } else {
                    $this->assign("error", "权限修改前后一致，无修改");
                }
            } else {
                $this->assign("error", "存在输入为空！");
            }
        }
        $this->display();
    }
}

